package org.redis.cluster.config;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;

public class SystemConfig implements Serializable {
	
	private Map<String,SystemPropertyConfig> configs;
	
	public SystemConfig(Collection<SystemPropertyConfig> systemPropertyConfigs) {
		this.configs=initPropertyConfigs(systemPropertyConfigs);
	}
	
	protected Map<String,SystemPropertyConfig> initPropertyConfigs(Collection<SystemPropertyConfig> systemPropertyConfigs) {
		if(CollectionUtils.isEmpty(systemPropertyConfigs)) {
			return MapUtils.EMPTY_MAP;
		}
		
		Map<String,SystemPropertyConfig> configs=new HashMap<String,SystemPropertyConfig>(systemPropertyConfigs.size());
		
		for(SystemPropertyConfig systemPropertyConfig:systemPropertyConfigs) {
			configs.put(systemPropertyConfig.getName(), systemPropertyConfig);
		}
		
		return configs;
	}

	public SystemPropertyConfig get(String name) {
		SystemPropertyConfig config=configs.get(name);
		if(config!=null) {
			return config;
		}
		
		return new SystemPropertyConfig(name, null);
	}

	public Collection<SystemPropertyConfig> getValues() {
		return configs.values();
	}

	@Override
	public String toString() {
		return String.format("{%s}", getValues());
	}
}